package com.gim.sap.sim;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.gim.sap.R;
import com.gim.sap.business.SIMBusiness;
import com.gim.sap.entity.SIMEntity;
import com.gim.sap.utility.service.SAPMService;

public class SIMMainActivity extends Activity {

	private ImageButton imgOK;
	private EditText etPWD;
	private EditText etPhone;
	private EditText etPWD2;
	private Context context =this;
	String phoneNumber; 
	String pwd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_simmain);
		initialControls();
		
	}
	private void initialControls() {
		etPWD =(EditText)findViewById(R.id.etPWD);
		etPhone=(EditText)findViewById(R.id.etBackPhone);
		etPWD2=(EditText)findViewById(R.id.etPWD2);
		imgOK=(ImageButton)findViewById(R.id.imgbtnOK);
		event();
	}
	private void event(){
		imgOK.setOnClickListener(myListener);
	}
	private View.OnClickListener myListener= new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
	
			if(!validation())
			{
				Toast.makeText(context,getResources().getString(R.string.SIMErrorMessage), Toast.LENGTH_LONG).show();
				return;
			}
			SIMBusiness bc = new SIMBusiness();
			TelephonyManager tlm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
			SIMEntity simEntity = new SIMEntity();
			simEntity.setPhoneNumber(phoneNumber);
			simEntity.setPwd(pwd);
			simEntity.setPhoneCode(bc.getPhoneCode(tlm));
			bc.doInsertBackPhone(context, simEntity);
			startMyServices();
		}
	};
	
	private void startMyServices(){
		Intent intent  = new Intent(this,SAPMService.class);
		intent.setAction("heaven.long.My.service");
		startService(intent);
	}
	
	
	private boolean validation( ){
		
		 phoneNumber = etPhone.getText().toString();
		 pwd = etPWD.getText().toString();
		String pwd2=etPWD2.getText().toString();
		if("".equals(phoneNumber))
			return false;
		if("".equals(pwd2))
			return false;
		if("".equals(pwd))
			return false;
		if(!pwd.equals(pwd2))
			return false;
		return true;
	}
}
